package ink.xiaobaibai.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import ink.xiaobaibai.bo.ClientOrderHaveCodeBo;
import ink.xiaobaibai.entity.ClientOrder;
import ink.xiaobaibai.vo.ClientOrderVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-05-21
 */
public interface ClientOrderDao extends BaseMapper<ClientOrder> {

    @Select("SELECT co.order_id,co.order_amount,co.device_id,co.create_time,co.run_time,od.`address`,od.`device_code` FROM client_order co LEFT JOIN ozone_device od ON od.id=co.`device_id` WHERE co.user_id=#{userId} ORDER BY co.create_time DESC LIMIT #{limitStart},#{limitEnd}")
    public List<ClientOrderVo> getClientListByUserId(@Param("userId") Integer userId, @Param("limitStart") Integer limitStart, @Param("limitEnd") Integer limitEnd);

    @Select("SELECT SUM(order_amount) FROM client_order WHERE device_id=#{deviceId}")
    public BigDecimal sumOrderAmount(@Param("deviceId") Integer deviceId);

    @Select("SELECT order_id,user_id,order_amount,create_time FROM client_order WHERE device_id=#{deviceId} ORDER BY create_time limit #{limitStart},#{limitEnd}")
    public List<ClientOrder> getOrderListByDeviceId(@Param("deviceId") Integer deviceId, @Param("limitStart") Integer limitStart, @Param("limitEnd") Integer limitEnd);

    @Select("SELECT cl.*,oz.`device_code` FROM client_order cl LEFT JOIN ozone_device oz ON oz.id=cl.`device_id` WHERE cl.`device_id`=#{deviceId} AND cl.create_time BETWEEN #{startTime} AND #{endTime} ORDER BY cl.create_time ASC")
    public List<ClientOrderHaveCodeBo> getOrderBoListByDeviceId(@Param("deviceId") Integer deviceId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select("SELECT cl.*,oz.`device_code` FROM client_order cl LEFT JOIN ozone_device oz ON oz.id=cl.`device_id` WHERE cl.`admin_user_id`=#{userId} AND cl.create_time BETWEEN #{startTime} AND #{endTime} ORDER BY cl.create_time ASC")
    public List<ClientOrderHaveCodeBo> getOrderBoListByAdminUserId(@Param("userId") Integer userId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select("SELECT cl.*,oz.`device_code` FROM client_order cl LEFT JOIN ozone_device oz ON oz.id=cl.`device_id` WHERE cl.`admin_dep_id`=#{depId} AND cl.create_time BETWEEN #{startTime} AND #{endTime} ORDER BY cl.create_time ASC")
    public List<ClientOrderHaveCodeBo> getOrderBoListByDepId(@Param("depId") Integer depId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

}
